﻿Public Class frmMercaderiaBuscador

    Private _codMercaderia As Integer = 0

    Public Property codMercaderia() As Integer
        Get
            Return _codMercaderia
        End Get
        Set(ByVal value As Integer)
            _codMercaderia = value
        End Set
    End Property

    Private _Mercaderia As String = ""

    Public Property MercaderiaDesc() As String
        Get
            Return _Mercaderia
        End Get
        Set(ByVal value As String)
            _Mercaderia = value
        End Set
    End Property

    Private Sub frmMercaderiaBuscador_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgMercaderia.AutoGenerateColumns = False
        CargarCombos()
        CargarMercaderias()

    End Sub

    Dim classController As New ControllerSigeco.controller_stock

    Private Sub CargarMercaderias()
        Try
            txtMercaderia.Focus()

            btnBuscar.Enabled = True

            Dim idMarca As Integer = 0
            If cbxMarca.Checked Then
                idMarca = cboMarca.SelectedValue
            End If

            Dim idRubro As Integer = 0
            If cbxRubro.Checked Then
                idRubro = cboRubro.SelectedValue
            End If

            Dim ds As New DataTable
            ds = classController.get_mercaderias_All(oiml.dbConn, 0, txtMercaderia.Text, idMarca, idRubro)

            If Not ds Is Nothing Then
                dgMercaderia.DataSource = ds
            Else
                dgMercaderia.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub CargarCombos()
        Dim dsMarca As New DataTable
        dsMarca = classController.get_marcas(oiml.dbConn, 0, "")
        If Not dsMarca Is Nothing Then
            cboMarca.DataSource = dsMarca
            cboMarca.DisplayMember = "marca"
            cboMarca.ValueMember = "idMarca"
        End If

        Dim dsRubro As New DataTable
        dsRubro = classController.get_rubros(oiml.dbConn, 0, "")
        If Not dsRubro Is Nothing Then
            cboRubro.DataSource = dsRubro
            cboRubro.DisplayMember = "rubro"
            cboRubro.ValueMember = "idRubro"
        End If

        'Dim dsImpuesto As New DataTable
        'dsImpuesto = classController.get_impuestos(oiml.dbConn, 0, "")
        'If Not dsImpuesto Is Nothing Then
        '    cboImpuesto.DataSource = dsImpuesto
        '    cboImpuesto.DisplayMember = "impuesto"
        '    cboImpuesto.ValueMember = "idImpuesto"
        'End If

    End Sub

    Private Sub seleccionar()
        Try

            If dgMercaderia.SelectedRows.Count > 0 Then

                codMercaderia = dgMercaderia.SelectedRows(0).Cells(0).Value
                MercaderiaDesc = dgMercaderia.SelectedRows(0).Cells(1).Value
                Me.Close()

            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        CargarMercaderias()

    End Sub

    Private Sub dgMercaderia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            seleccionar()
        End If
    End Sub

    Private Sub dgMercaderia_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMercaderia.DoubleClick
        seleccionar()
    End Sub

    Private Sub txtMarca_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMercaderias()
        End If
    End Sub

End Class